<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Userrights extends TMT_Controller 
{
	public function __construct()
	{
		parent::__construct();
                $this->load->library('form_validation');
                $this->load->helper('form');
                
	}
	
	
	public function index()
	{
		$this->headline = 'Benutzerrechte';
		
		$this->load->library('table');
		$tabletemplate 	 = array('table_open' 	=> '<table class="table table-bordered table-striped">',
				'row_start' 	=> '<tr class="tr_alt">',
				'row_alt_start' => '<tr>'
		);
		
		$this->table->set_template($tabletemplate);
		
		$roletypes = Model\Roletype::all(); 
		$roletype_arr = array();
		$roletype_arr[] = 'Controller';
		$roletype_arr[] = 'Function';
		foreach($roletypes as $roletype)
		{
			$roletype_arr[] = $roletype->name;
		}
		$roletype_arr[] = '';
		$this->table->set_heading($roletype_arr);
		
		
		$path = dirname(BASEPATH).'/application/controllers/';
		
		$controller = array();
		//$controller[] = '';
		
		$dirhandle = opendir($path);
		if($dirhandle)
		{
			while (false !== ($file = readdir($dirhandle))) {
					if($file[0] === '.')
						continue;
					
					
					$filename_extension = explode('.',$file,2);
					
					if(isset($filename_extension[1]) && $filename_extension[1] === 'php')
						$controller[] = $filename_extension[0];
	    		}
		}
		else 
		{
			echo 'Handle not open!';	
		}
		
		
		$data = array();
		foreach($controller as $key => $controller_item)
		{
			
			$handle = @fopen($path."/".$controller_item.".php", "r");
			
			if($handle)
			{
				while(($buffer = fgets($handle, 4096)) !== FALSE)
				{
					if(preg_match("/^\s*(function|public function) (.*)\(.*\).*$/", $buffer, $result) === 1)
					{
						if(substr($result[2], 0, 1) !== '_')
							$data[$controller_item][$result[2]] = array();
					}
				}
				if (!feof($handle)) {
					echo "Fehler: unerwarteter fgets() Fehlschlag\n";
				}
				fclose($handle);
			}
			
		}
		
		
		$userrights = Model\Navitem::all();
		
		foreach($userrights as $userright)
		{
			if($userright->function == '')
				$function = 'index';
			else
				$function = $userright->function; 
			$data[$userright->controller][$function][$userright->roletype_id] = 1;
			
		}
		
		foreach($controller as $item)
		{
			$this->table->add_row($item,'','<input type="checkbox" name="test" />','<input type="checkbox" name="test" />','<input type="checkbox" name="test" />','<input type="checkbox" name="test" />','<input type="submit" class="btn btn-small" value="Speichern"/>');
		}
                
                /**
                 * @author Felix Busch
                 * 05.02.2013
                 */
//                $roles = Model\Roletype::select('roletype_id')->distinct()->all();
//                $this->view_data['roles'] = $roles;
//                
//                foreach($roles as $role)
//                {
//                    $nav_data[$role->roletype_id] = Model\Userright::where('roletype_id', $role->roletype_id)->all();
//                }
//                
//                $this->view_data['nav_data'] = $nav_data;
                $this->view_data['roles'] = Model\Roletype::all();
                
                $this->view_data['roles_array'] = Model\Roletype::get_assoc_array();
                
                $this->view_data['selected_tab'] = $this->session->flashdata('selected_tab');
                
		
	}
        
        public function add_new()
        {            
            $this->form_validation->set_rules('roletype', 'Rolle', 'required');
            $this->form_validation->set_rules('label', 'Beschriftung', 'required|xss_clean');
            $this->form_validation->set_rules('controller', 'Password Confirmation', 'required|xss_clean');
            $this->form_validation->set_rules('function', 'Email', 'xss_clean');
            $this->form_validation->set_rules('prio', 'Email', 'required|xss_clean|alpha_numeric');
            
                if (!$this->form_validation->run())
		{
                    redirect('userrights');
		}
		else
		{
                    $visible = $this->input->post('visible');
                    if($visible === 'on') $visible = TRUE;
                    else $visible = FALSE;                    
                    
                    $navitem = new Model\Navitem();
                    $navitem->name = $this->form_validation->set_value('label');
                    $navitem->controller = $this->form_validation->set_value('controller');
                    $navitem->function = $this->form_validation->set_value('function');
                    $navitem->priority = $this->form_validation->set_value('prio');
                    $navitem->menu = $visible;
                    
                    $navitem->related->set('entities.roletype.pivot', array('roletype_id' => $this->form_validation->set_value('roletype')));

                    $navitem->save();
                    
                    $this->session->set_flashdata('selected_tab', TRUE);
                    
                    redirect('userrights');
		}
            
        }
        
}
